Sorting Visualizer

Speed

Bubble Sort

Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the intended order. It is called bubble sort because the movement of array elements is just like the movement of air bubbles in the water. Bubbles in water rise up to the surface; similarly, the array elements in bubble sort move to the end in each iteration.Although it is simple to use, it is primarily used as an educational tool because the performance of bubble sort is poor in the real world. It is not suitable for large data sets. The average and worst-case complexity of Bubble sort is O(n2), where n is a number of items.

Working of Bubble Sort

Suppose we are trying to sort the elements in ascending order.

1. First Iteration (Compare and Swap)
  1. 1)Starting from the first index, compare the first and the second elements.
  2. 2)If the first element is greater than the second element, they are swapped.
  3. 3)Now, compare the second and the third elements. Swap them if they are not in order.
  4. 4)The above process goes on until the last element.
2. Remaining Iteration
  1. 1)The same process goes on for the remaining iterations.
  2. 2)After each iteration, the largest element among the unsorted elements is placed at the end.